# from http.client import HTTPResponse
import os.path

from django.shortcuts import render, HttpResponse, redirect
from imageio.v3 import improps
import json
from pathlib import Path
# Create your views here.

data_path = Path(__file__).parent / "data"


def index(request):  # 默认有个request参数
    return HttpResponse("欢迎使用")


def index2(request):  # 默认有个request参数
    return HttpResponse("欢迎使用1")  # 返回文本


def write_json(data, path):
    with open(path, 'w', encoding='utf -8') as f:
        json.dump(data, f, ensure_ascii=False, indent=4)


def user_list(request):
    # 去app目录下的templates下寻找 user_list.html(根据app的注册顺序去找
    return render(request, "user_list.html")


def tpl(request):
    n1 = "WangXin"
    n2 = ["管理员", "CEO", "CPO"]
    user_info = {"name": "Wang", "salary": 1000, "gender": "man"}
    data_list = [
        {"name": "Wang", "salary": 1000, "gender": "man"},
        {"name": "chen", "salary": 1000, "gender": "woman"},
        {"name": "li", "salary": 1000, "gender": "man"}
    ]
    return render(request, 'tpl.html', {"n1": n1, "n2": n2, "n3": user_info, "n4": data_list})


def news(req):
    # 1.定义一些数据或者从数据库中获取或者通过爬虫请求
    # 2.https://www.10086.cn/aboutus/news/groupnews/5018449_5585_11769.json?nowtime=1760455515532发送请求
    # request模块
    import requests
    res = requests.get("https://www.10086.cn/aboutus/news/groupnews/5018449_5585_11769.json?nowtime=1760455515532")
    data_list = res.json()
    print(data_path / "news.json")
    print(os.path.exists(data_path / "news.json"))

    # # 检查几种可能的data路径
    # possible_paths = [
    #     Path("/data"),  # 绝对路径，不行
    #     Path("data"),  # 相对当前工作目录，不行
    #     Path(__file__).resolve().parent / "data",  # 相对应用目录,可以
    # ]

    # for path in possible_paths:
    #     news_file = path / "news.json"
    #     exists = os.path.exists(news_file)
    #     print(f"路径: {news_file} -> 存在: {exists}")
    # write_json(data_list,data_path/"news.json")
    print("已经读取完毕")
    # print(data_list)
    return render(req, "news.html", {"data_list": data_list["cData"]["list"]})


def wangyi(req):
    import requests
    res = requests.get("https://buff.163.com/api/market/goods?game=csgo&_=1760540204044")
    data_list = res.json()
    # print(data_path / "wangyi.json")
    write_json(data_list, data_path / "wangyi.json")
    # print(os.path.exists(data_path / "news.json"))
    return render(req, "wangyi.html", {"data_list": data_list["data"]["items"]})


def qingqiu(request):
    # request是一个对象，封装了用户发来的所有请求相关的信息
    # 1.获取请求方式 GET/POST
    print(request.method)

    # 2.在URL上传递值 例如 /qingqiu/?name=wangxin
    print(request.GET)

    # 3.在请求体中国提交的数据
    print(request.POST)
    return HttpResponse("返回内容")
    # 4，return HtppResonse("")返回一个字符串
    # 5. return rende(request, "tpl.html") 返回一个html文件
    # 6 return redirect("") 重定向到一个新的url
    # 7 return


def login(request):
    if request.method == "POST":
        username = request.POST.get("user")
        password = request.POST.get("password")
        print(username, password)
        if username == "wangxin" and password == "123":
            # return HttpResponse("登录成功")
            return redirect("https://www.10086.cn/")
        else:
            # return HttpResponse("用户名或密码错误")
            return render(request, "login.html", {"error": "用户名或密码错误"})
    else:

        return render(request, "login.html")
